package tq.com.tqteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import tq.com.systools.system.StringHelper;
import tq.com.tqteacher.R;

public class TQInputView1 extends FrameLayout {
    private int inputType;
    private int iconRes;
    private String title;

    private boolean showRightIcon = false;
    private int righIconRes;
    private String hint;

    private TextView titleView;
    private ImageView iconView;
    private EditText mEditText;
    private View rightIcon;

    private boolean hidepwd = true;
    public TQInputView1(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    private void initView(AttributeSet pAttributeSet){


        TypedArray typedArray = getContext().obtainStyledAttributes(pAttributeSet, R.styleable.TQInputView1);
        inputType = typedArray.getInt(R.styleable.TQInputView1_TQInputView1_type, 0);
        iconRes = typedArray.getResourceId(R.styleable.TQInputView1_TQInputView1_icon, 0);
        title = typedArray.getString(R.styleable.TQInputView1_TQInputView1_title);
//        showRightIcon = typedArray.getBoolean(R.styleable.TQInputView1_TQInputView1_right_icon_show, false);
        righIconRes = typedArray.getResourceId(R.styleable.TQInputView1_TQInputView1_right_icon, 0);
        hint = typedArray.getString(R.styleable.TQInputView1_TQInputView1_hint);

        typedArray.recycle();

        if(inputType == 2){
            LayoutInflater.from(getContext()).inflate(R.layout.view_get_valid_code_layout, this, true);
        }else{
            LayoutInflater.from(getContext()).inflate(R.layout.view_input_layout, this, true);
        }

        titleView = findViewById(R.id.title);
        iconView = findViewById(R.id.icon);
        mEditText = findViewById(R.id.input);

        rightIcon = findViewById(R.id.right_icon);

        if(!StringHelper.isEmpty(title)){
            titleView.setText(title);
        }
        if(!StringHelper.isEmpty(hint)){
            mEditText.setHint(hint);
        }
//        rightIcon.setVisibility(showRightIcon ? VISIBLE : GONE);
        if(inputType == 0){
            rightIcon.setVisibility(GONE);
        }else{
            rightIcon.setVisibility(VISIBLE);
        }

        if(righIconRes == 1){
            ((ImageView)rightIcon).setImageResource(righIconRes);
        }

        if(iconRes != 0){
            iconView.setImageResource(iconRes);
        }


        if(inputType == 1){
            mEditText.setInputType(hidepwd ? InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT : InputType.TYPE_CLASS_TEXT);
            hidepwd = true;
            ((ImageView)rightIcon).setImageResource(hidepwd ? R.drawable.icon_yc : R.drawable.icon_xs);

            rightIcon.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    hidepwd = !hidepwd;
                    mEditText.setInputType(hidepwd ? InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT : InputType.TYPE_CLASS_TEXT);

                    ((ImageView)rightIcon).setImageResource(hidepwd ? R.drawable.icon_yc : R.drawable.icon_xs);

                    if(StringHelper.hasText(mEditText)){
                        mEditText.setSelection(mEditText.getText().length());
                    }

                }
            });

//            mEditText.setMax(32);
        }else if(inputType == 2){
            mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
            rightIcon.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

    public String getString(){
        return StringHelper.editText(mEditText);
    }

    public void setString(String txt){
        mEditText.setText(txt);
    }
    public void setRightClickListener(OnClickListener pOnClickListener){
//        rightIcon.setOnClickListener(pOnClickListener);
    }

    public void setMaxLength(int nu){
        InputFilter[] lFilters = {new InputFilter.LengthFilter(nu)};
        mEditText.setFilters(lFilters);
    }
}
